package edu.usm.predatorwars;

import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.simple.container.SimpleServerFactory;

import edu.usm.predatorwars.webservice.ClientMaskResource;

/**
 * This class can create a local server that exposes web services to
 * the game host.
 */
public class LocalWebServerImpl extends GameEventProducer {
	
	private final static Logger log = Logger.getLogger(ClientMask.class);
	private String hostAddress;
	
	public LocalWebServerImpl(GameEventConsumer consumer, String hostAddress) {
		super(consumer);
		this.hostAddress = hostAddress;
	}
	
	public void start() {
        try {
        	DefaultResourceConfig config = new DefaultResourceConfig();
        	Map<String, Boolean> features = config.getFeatures();
        	features.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
        	System.out.println("start");
        	ClientMaskResource resource = new ClientMaskResource(consumer);
        	config.getSingletons().add(resource);
        	
			SimpleServerFactory.create("http://" + hostAddress, config);
		} catch (IllegalArgumentException e) {
			log.fatal("Could not start local web server:" + e.getMessage());
			
		} catch (IOException e) {
			log.fatal("Could not start local web server:" + e.getMessage());
		}
	}
}
